<template>
  <div class="showfooditem" v-loading="loading">
    <div class="showfood-box">
      <div class="choose" ref="choose">
        <div
          class="content"
          ref="content"
          @mousemove="handleMove"
          @mouseout="handleOut"
        >
          <img :src="foodList.fimg" />
          <div class="shadow" ref="shadow"></div>
        </div>
        <ul id="listshow">
          <li>
            <img :src="foodList.fimg" alt="" />
          </li>
        </ul>
      </div>

      <div class="larger" ref="larger">
        <img :src="foodList.fimg" ref="big" />
      </div>

      <div class="showfood-right">
        <div class="wwwwimg">
          <div v-if="love">
            <img @click="openCenter" src="../../assets/img/love.svg" alt="" />
          </div>
          <div v-else>
            <img @click="openCenter" src="../../assets/img/nolove.svg" alt="" />
          </div>
        </div>

        <div>
          {{ foodList.fname }}
        </div>
        <div>类别 : {{ foodList.ftName }}</div>

        <div>
          {{ foodList.fcontent }}
        </div>

        <div>制作方法 :</div>

        <div class="wenziss">
          <div v-html="foodList.fmake"></div>
        </div>

        <div class="showfood-fang">访问量 : {{ foodList.fvisits }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { request } from "../../network/request";
export default {
  name: "ShowFoodItem",
  data() {
    return {
      love: false,
      foodList: "",
      userid: "",
      fid: "",
      loading: true,
    };
  },
  mounted() {
    if (
      sessionStorage.getItem("userid") == "null" ||
      sessionStorage.getItem("userid") == null
    ) {
      this.userid = "";
      console.log(this.userid);
    } else {
      this.userid = sessionStorage.getItem("userid");
    }
    this.fid = this.$route.query.fid;
    request({
      url: "/foot/queryAllFootByFId",
      params: {
        fId: this.$route.query.fid,
        userId: this.userid,
      },
    })
      .then((res) => {
        this.love = res.data.data.efstate;
        this.foodList = res.data.data;
        (this.loading = false), console.log(res.data.data);
        console.log(res.data.data.efstate);
      })
      .catch((err) => {
        console.log(err);
      });

    request({
      url: "/foot/updateVisits",
      params: {
        fId: this.$route.query.fid,
      },
    })
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.log(err);
      });

    if (
      sessionStorage.getItem("userid") != "null" &&
      sessionStorage.getItem("userid") != null
    ) {
      request({
        url: "/user/updateHistory",
        params: {
          fId: this.fid,
          userId: sessionStorage.getItem("userid"),
        },
      })
        .then((res) => {
          console.log(this.fid);
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
    }
  },
  methods: {
    // 获取元素到文档区域的坐标
    getPosition: function (element) {
      var dc = document,
        rec = element.getBoundingClientRect(),
        x = rec.left, // 获取元素相对浏览器视窗window的左、上坐标
        y = rec.top;
      // 与html或body元素的滚动距离相加就是元素相对于文档区域document的坐标位置
      x += dc.documentElement.scrollLeft || dc.body.scrollLeft;
      y += dc.documentElement.scrollTop || dc.body.scrollTop;
      return {
        left: x,
        top: y,
      };
    },
    handleMove(evt) {
      var larger = this.$refs.larger;
      var shadow = this.$refs.shadow;
      var big = this.$refs.big;
      var pos = this.getPosition(this.$refs.choose);
      var shadowRect = shadow.getBoundingClientRect();
      var bigRect = big.getBoundingClientRect();
      var contentRect = this.$refs.content.getBoundingClientRect();
      var maxX = contentRect.width - shadowRect.width;
      var maxY = contentRect.height - shadowRect.height;

      var X = evt.pageX - pos.left - shadowRect.width / 2;
      var Y = evt.pageY - pos.top - shadowRect.height / 2;

      if (X <= 0) {
        X = 0;
      }
      if (X >= maxX) {
        X = maxX;
      }
      if (Y <= 0) {
        Y = 0;
      }
      if (Y >= maxY) {
        Y = maxY;
      }
      // 防止遮罩层粘滞，跟随鼠标一起滑出大图位置
      var bigX = (X * bigRect.width) / contentRect.width;
      var bigY = (Y * bigRect.height) / contentRect.height;
      //  bigX / bigW = X / contentW,主图和遮罩层之间存在两倍关系，放大图和原图之间也有两倍关系
      shadow.style.left = X + "px";
      shadow.style.top = Y + "px";

      // console.log(X, Y, bigX, bigY);

      big.style.left = -bigX + "px";
      big.style.top = -bigY + "px";

      larger.style.display = "block";
      shadow.style.display = "block";
    },
    handleOut() {
      var larger = this.$refs.larger;
      var shadow = this.$refs.shadow;
      larger.style.display = "none";
      shadow.style.top = "-1000px";
    },

    openCenter() {
      if (
        sessionStorage.getItem("userid") == null ||
        sessionStorage.getItem("userid") == "null"
      ) {
        this.$confirm("未登录是否, 是否登录?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$router.push("/loginpage");
            location.reload();
          })
          .catch(() => {
            this.$message({
              type: "info",
              iconClass: "ss",
              message: "已取消登录",
            });
          });
      } else {
        if (this.love) {
          this.love = 0;
        } else {
          this.love = 1;
        }

        request({
          url: "/enjoyfoot/isState",
          params: {
            fId: this.$route.query.fid,
            efState: this.love,
            userId: sessionStorage.getItem("userid"),
          },
        })
          .then((res) => {
            this.love = res.data.data;
            console.log(res, res.data.data);
          })
          .catch((err) => {
            console.log(err);
          });
        if (this.love) {
          this.$message({
            message: "已添加至喜欢",
            iconClass: "ss",
            center: false,
            // close:()=>{
            //   location.reload()
            // }
          });
          this.love = !this.love;
        } else {
          this.$message({
            message: "已取消喜欢",
            iconClass: "ss",
            center: false,
            // close:()=>{
            //   location.reload()
            // }
          });
          this.love = !this.love;
        }
      }
    },
  },
};
</script>

<style>
.showfooditem {
  padding-top: 1.1rem;
}

.showfood-box {
  width: 90%;
  margin-left: 0.8rem;
  display: flex;
}

.choose {
  width: 3.4rem;
}

.content {
  width: 3.4rem;
  height: 2.3rem;
  position: relative;
}

.content img {
  width: 3.4rem;
  height: 2.3rem;
}
#listshow {
  width: 3.4rem;
  list-style: none;
}

#listshow li {
  width: 1.2rem;
  height: 0.8rem;
}

#listshow li img {
  width: 100%;
  height: 100%;
  border: 1px solid saddlebrown;
}

.larger {
  width: 1.9rem;
  height: 1.3rem;
  position: absolute;
  left: 4.3rem;
  float: left;
  overflow: hidden;
  display: none;
}

.larger img {
  width: 5rem;
  height: 3rem;
  position: absolute;
  left: 0;
  top: 0;
}

.showfood-fang {
  font-size: 0.1rem;
  margin-left: 0.5rem;
  margin-top: 0.3rem;
  width: 3.5rem;
}

.shadow {
  width: 1.2rem;
  height: 1rem;
  background-color: rgba(145, 200, 200, 0.4);
  position: absolute;
  left: 0;
  top: 0;
  z-index: 99;
  display: none;
}

.wwwwimg img {
  width: 0.25rem;
  position: absolute;
  right: 0.8rem;
  cursor: pointer;
}

.showfood-right :nth-child(2) {
  width: 4.5rem;
  font-size: 0.15rem;
  margin-left: 0.5rem;
}

.showfood-right :nth-child(3) {
  width: 4.5rem;
  font-size: 0.1rem;
  margin-left: 0.5rem;
  margin-top: 0.15rem;
}

.showfood-right :nth-child(4) {
  width: 4.5rem;
  font-size: 0.1rem;
  margin-left: 0.5rem;
  margin-top: 0.2rem;
}

.showfood-right :nth-child(5) {
  width: 4.5rem;
  font-size: 0.115rem;
  margin-left: 0.5rem;
  margin-top: 0.3rem;
}

.showfood-right :nth-child(6) {
  width: 4.5rem;
  line-height: 0.2rem;
  font-size: 0.1rem;
  margin-left: 0.5rem;
  margin-top: 0.15rem;
}
</style>